package online.siwu.memorycards


import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_start.*
import java.util.*

class StartActivity : AppCompatActivity() {

    val GD = GlobalData

    val random = Random()

    override fun onCreate(state: Bundle?) {
        super.onCreate(state)
        setContentView(R.layout.activity_start)

        btStart.setOnClickListener (oclStart)
    }

    val oclStart = View.OnClickListener {
        if (checkNumber()) {
            GD.exerNum = etNumber.text.toString().toInt()
            initArray(GD.exerNum)
            gotoActivity<MainActivity>()
        }
    }

    private fun initArray(num: Int) {
        GD.arrExer = arrayOfNulls<Int>(num)
        for(i in GD.arrExer.indices )
            GD.arrExer[i] = random.nextInt(110)

        GD.arrAnswer = arrayOfNulls<Int>(num) //这个是Answer的
    }

    private fun checkNumber(): Boolean {

        val i = etNumber.text.toString()
        if (i == "")
            etNumber.setText(100.toString())
        else if (i.toInt() < 10)
            etNumber.setText(10.toString())
        else if (i.toInt() > 200)
            etNumber.setText(200.toString())
        else
            return true

        toast("请输入数值在10-200之间")
        return false
    }
}
